home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Musique / solfege / solfege-win32-3.17.0.exe / {app} / bin / Lib / bsddb / test / test_dbtables.py < prev    next >
Text File  |  2007-12-10  |  14KB  |  379 lines

  1. #!/usr/bin/env python
  2. #
  3. #-----------------------------------------------------------------------
  4. # A test suite for the table interface built on bsddb.db
  5. #-----------------------------------------------------------------------
  6. #
  7. # Copyright (C) 2000, 2001 by Autonomous Zone Industries
  8. # Copyright (C) 2002 Gregory P. Smith
  9. #
  10. # March 20, 2000
  11. #
  12. # License:      This is free software.  You may use this software for any
  13. #               purpose including modification/redistribution, so long as
  14. #               this header remains intact and that you do not claim any
  15. #               rights of ownership or authorship of this software.  This
  16. #               software has been tested, but no warranty is expressed or
  17. #               implied.
  18. #
  19. #   --  Gregory P. Smith <greg@electricrain.com>
  20. #
  21. # $Id: test_dbtables.py 58536 2007-10-18 17:15:20Z gregory.p.smith $
  22.  
  23. import sys, os, re
  24. import shutil
  25. import tempfile
  26. try:
  27.     import cPickle as pickle
  28. except ImportError:
  29.     import pickle
  30.  
  31. import unittest
  32. from test_all import verbose
  33.  
  34. try:
  35.     # For Pythons w/distutils pybsddb
  36.     from bsddb3 import db, dbtables
  37. except ImportError:
  38.     # For Python 2.3
  39.     from bsddb import db, dbtables
  40.  
  41.  
  42.  
  43. #----------------------------------------------------------------------
  44.  
  45. class TableDBTestCase(unittest.TestCase):
  46.     def setUp(self):
  47.         homeDir = tempfile.mkdtemp()
  48.         self.testHomeDir = homeDir
  49.         try: os.mkdir(homeDir)
  50.         except os.error: pass
  51.         self.tdb = dbtables.bsdTableDB(
  52.             filename='tabletest.db', dbhome=homeDir, create=1)
  53.  
  54.     def tearDown(self):
  55.         self.tdb.close()
  56.         shutil.rmtree(self.testHomeDir)
  57.  
  58.     def test01(self):
  59.         tabname = "test01"
  60.         colname = 'cool numbers'
  61.         try:
  62.             self.tdb.Drop(tabname)
  63.         except dbtables.TableDBError:
  64.             pass
  65.         self.tdb.CreateTable(tabname, [colname])
  66.         self.tdb.Insert(tabname, {colname: pickle.dumps(3.14159, 1)})
  67.  
  68.         if verbose:
  69.             self.tdb._db_print()
  70.  
  71.         values = self.tdb.Select(
  72.             tabname, [colname], conditions={colname: None})
  73.  
  74.         colval = pickle.loads(values[0][colname])
  75.         assert(colval > 3.141 and colval < 3.142)
  76.  
  77.  
  78.     def test02(self):
  79.         tabname = "test02"
  80.         col0 = 'coolness factor'
  81.         col1 = 'but can it fly?'
  82.         col2 = 'Species'
  83.         testinfo = [
  84.             {col0: pickle.dumps(8, 1), col1: 'no', col2: 'Penguin'},
  85.             {col0: pickle.dumps(-1, 1), col1: 'no', col2: 'Turkey'},
  86.             {col0: pickle.dumps(9, 1), col1: 'yes', col2: 'SR-71A Blackbird'}
  87.         ]
  88.  
  89.         try:
  90.             self.tdb.Drop(tabname)
  91.         except dbtables.TableDBError:
  92.             pass
  93.         self.tdb.CreateTable(tabname, [col0, col1, col2])
  94.         for row in testinfo :
  95.             self.tdb.Insert(tabname, row)
  96.  
  97.         values = self.tdb.Select(tabname, [col2],
  98.             conditions={col0: lambda x: pickle.loads(x) >= 8})
  99.  
  100.         assert len(values) == 2
  101.         if values[0]['Species'] == 'Penguin' :
  102.             assert values[1]['Species'] == 'SR-71A Blackbird'
  103.         elif values[0]['Species'] == 'SR-71A Blackbird' :
  104.             assert values[1]['Species'] == 'Penguin'
  105.         else :
  106.             if verbose:
  107.                 print "values= %r" % (values,)
  108.             raise "Wrong values returned!"
  109.  
  110.     def test03(self):
  111.         tabname = "test03"
  112.         try:
  113.             self.tdb.Drop(tabname)
  114.         except dbtables.TableDBError:
  115.             pass
  116.         if verbose:
  117.             print '...before CreateTable...'
  118.             self.tdb._db_print()
  119.         self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])
  120.         if verbose:
  121.             print '...after CreateTable...'
  122.             self.tdb._db_print()
  123.         self.tdb.Drop(tabname)
  124.         if verbose:
  125.             print '...after Drop...'
  126.             self.tdb._db_print()
  127.         self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])
  128.  
  129.         try:
  130.             self.tdb.Insert(tabname,
  131.                             {'a': "",
  132.                              'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
  133.                              'f': "Zero"})
  134.             assert 0
  135.         except dbtables.TableDBError:
  136.             pass
  137.  
  138.         try:
  139.             self.tdb.Select(tabname, [], conditions={'foo': '123'})
  140.             assert 0
  141.         except dbtables.TableDBError:
  142.             pass
  143.  
  144.         self.tdb.Insert(tabname,
  145.                         {'a': '42',
  146.                          'b': "bad",
  147.                          'c': "meep",
  148.                          'e': 'Fuzzy wuzzy was a bear'})
  149.         self.tdb.Insert(tabname,
  150.                         {'a': '581750',
  151.                          'b': "good",
  152.                          'd': "bla",
  153.                          'c': "black",
  154.                          'e': 'fuzzy was here'})
  155.         self.tdb.Insert(tabname,
  156.                         {'a': '800000',
  157.                          'b': "good",
  158.                          'd': "bla",
  159.                          'c': "black",
  160.                          'e': 'Fuzzy wuzzy is a bear'})
  161.  
  162.         if verbose:
  163.             self.tdb._db_print()
  164.  
  165.         # this should return two rows
  166.         values = self.tdb.Select(tabname, ['b', 'a', 'd'],
  167.             conditions={'e': re.compile('wuzzy').search,
  168.                         'a': re.compile('^[0-9]+$').match})
  169.         assert len(values) == 2
  170.  
  171.         # now lets delete one of them and try again
  172.         self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')})
  173.         values = self.tdb.Select(
  174.             tabname, ['a', 'd', 'b'],
  175.             conditions={'e': dbtables.PrefixCond('Fuzzy')})
  176.         assert len(values) == 1
  177.         assert values[0]['d'] == None
  178.  
  179.         values = self.tdb.Select(tabname, ['b'],
  180.             conditions={'c': lambda c: c == 'meep'})
  181.         assert len(values) == 1
  182.         assert values[0]['b'] == "bad"
  183.  
  184.  
  185.     def test04_MultiCondSelect(self):
  186.         tabname = "test04_MultiCondSelect"
  187.         try:
  188.             self.tdb.Drop(tabname)
  189.         except dbtables.TableDBError:
  190.             pass
  191.         self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])
  192.  
  193.         try:
  194.             self.tdb.Insert(tabname,
  195.                             {'a': "",
  196.                              'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
  197.                              'f': "Zero"})
  198.             assert 0
  199.         except dbtables.TableDBError:
  200.             pass
  201.  
  202.         self.tdb.Insert(tabname, {'a': "A", 'b': "B", 'c': "C", 'd': "D",
  203.                                   'e': "E"})
  204.         self.tdb.Insert(tabname, {'a': "-A", 'b': "-B", 'c': "-C", 'd': "-D",
  205.                                   'e': "-E"})
  206.         self.tdb.Insert(tabname, {'a': "A-", 'b': "B-", 'c': "C-", 'd': "D-",
  207.                                   'e': "E-"})
  208.  
  209.         if verbose:
  210.             self.tdb._db_print()
  211.  
  212.         # This select should return 0 rows.  it is designed to test
  213.         # the bug identified and fixed in sourceforge bug # 590449
  214.         # (Big Thanks to "Rob Tillotson (n9mtb)" for tracking this down
  215.         # and supplying a fix!!  This one caused many headaches to say
  216.         # the least...)
  217.         values = self.tdb.Select(tabname, ['b', 'a', 'd'],
  218.             conditions={'e': dbtables.ExactCond('E'),
  219.                         'a': dbtables.ExactCond('A'),
  220.                         'd': dbtables.PrefixCond('-')
  221.                        } )
  222.         assert len(values) == 0, values
  223.  
  224.  
  225.     def test_CreateOrExtend(self):
  226.         tabname = "test_CreateOrExtend"
  227.  
  228.         self.tdb.CreateOrExtendTable(
  229.             tabname, ['name', 'taste', 'filling', 'alcohol content', 'price'])
  230.         try:
  231.             self.tdb.Insert(tabname,
  232.                             {'taste': 'crap',
  233.                              'filling': 'no',
  234.                              'is it Guinness?': 'no'})
  235.             assert 0, "Insert should've failed due to bad column name"
  236.         except:
  237.             pass
  238.         self.tdb.CreateOrExtendTable(tabname,
  239.                                      ['name', 'taste', 'is it Guinness?'])
  240.  
  241.         # these should both succeed as the table should contain the union of both sets of columns.
  242.         self.tdb.Insert(tabname, {'taste': 'crap', 'filling': 'no',
  243.                                   'is it Guinness?': 'no'})
  244.         self.tdb.Insert(tabname, {'taste': 'great', 'filling': 'yes',
  245.                                   'is it Guinness?': 'yes',
  246.                                   'name': 'Guinness'})
  247.  
  248.  
  249.     def test_CondObjs(self):
  250.         tabname = "test_CondObjs"
  251.  
  252.         self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e', 'p'])
  253.  
  254.         self.tdb.Insert(tabname, {'a': "the letter A",
  255.                                   'b': "the letter B",
  256.                                   'c': "is for cookie"})
  257.         self.tdb.Insert(tabname, {'a': "is for aardvark",
  258.                                   'e': "the letter E",
  259.                                   'c': "is for cookie",
  260.                                   'd': "is for dog"})
  261.         self.tdb.Insert(tabname, {'a': "the letter A",
  262.                                   'e': "the letter E",
  263.                                   'c': "is for cookie",
  264.                                   'p': "is for Python"})
  265.  
  266.         values = self.tdb.Select(
  267.             tabname, ['p', 'e'],
  268.             conditions={'e': dbtables.PrefixCond('the l')})
  269.         assert len(values) == 2, values
  270.         assert values[0]['e'] == values[1]['e'], values
  271.         assert values[0]['p'] != values[1]['p'], values
  272.  
  273.         values = self.tdb.Select(
  274.             tabname, ['d', 'a'],
  275.             conditions={'a': dbtables.LikeCond('%aardvark%')})
  276.         assert len(values) == 1, values
  277.         assert values[0]['d'] == "is for dog", values
  278.         assert values[0]['a'] == "is for aardvark", values
  279.  
  280.         values = self.tdb.Select(tabname, None,
  281.                                  {'b': dbtables.Cond(),
  282.                                   'e':dbtables.LikeCond('%letter%'),
  283.                                   'a':dbtables.PrefixCond('is'),
  284.                                   'd':dbtables.ExactCond('is for dog'),
  285.                                   'c':dbtables.PrefixCond('is for'),
  286.                                   'p':lambda s: not s})
  287.         assert len(values) == 1, values
  288.         assert values[0]['d'] == "is for dog", values
  289.         assert values[0]['a'] == "is for aardvark", values
  290.  
  291.     def test_Delete(self):
  292.         tabname = "test_Delete"
  293.         self.tdb.CreateTable(tabname, ['x', 'y', 'z'])
  294.  
  295.         # prior to 2001-05-09 there was a bug where Delete() would
  296.         # fail if it encountered any rows that did not have values in
  297.         # every column.
  298.         # Hunted and Squashed by <Donwulff> (Jukka Santala - donwulff@nic.fi)
  299.         self.tdb.Insert(tabname, {'x': 'X1', 'y':'Y1'})
  300.         self.tdb.Insert(tabname, {'x': 'X2', 'y':'Y2', 'z': 'Z2'})
  301.  
  302.         self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')})
  303.         values = self.tdb.Select(tabname, ['y'],
  304.                                  conditions={'x': dbtables.PrefixCond('X')})
  305.         assert len(values) == 0
  306.  
  307.     def test_Modify(self):
  308.         tabname = "test_Modify"
  309.         self.tdb.CreateTable(tabname, ['Name', 'Type', 'Access'])
  310.  
  311.         self.tdb.Insert(tabname, {'Name': 'Index to MP3 files.doc',
  312.                                   'Type': 'Word', 'Access': '8'})
  313.         self.tdb.Insert(tabname, {'Name': 'Nifty.MP3', 'Access': '1'})
  314.         self.tdb.Insert(tabname, {'Type': 'Unknown', 'Access': '0'})
  315.  
  316.         def set_type(type):
  317.             if type == None:
  318.                 return 'MP3'
  319.             return type
  320.  
  321.         def increment_access(count):
  322.             return str(int(count)+1)
  323.  
  324.         def remove_value(value):
  325.             return None
  326.  
  327.         self.tdb.Modify(tabname,
  328.                         conditions={'Access': dbtables.ExactCond('0')},
  329.                         mappings={'Access': remove_value})
  330.         self.tdb.Modify(tabname,
  331.                         conditions={'Name': dbtables.LikeCond('%MP3%')},
  332.                         mappings={'Type': set_type})
  333.         self.tdb.Modify(tabname,
  334.                         conditions={'Name': dbtables.LikeCond('%')},
  335.                         mappings={'Access': increment_access})
  336.  
  337.         try:
  338.             self.tdb.Modify(tabname,
  339.                             conditions={'Name': dbtables.LikeCond('%')},
  340.                             mappings={'Access': 'What is your quest?'})
  341.         except TypeError:
  342.             # success, the string value in mappings isn't callable
  343.             pass
  344.         else:
  345.             raise RuntimeError, "why was TypeError not raised for bad callable?"
  346.  
  347.         # Delete key in select conditions
  348.         values = self.tdb.Select(
  349.             tabname, None,
  350.             conditions={'Type': dbtables.ExactCond('Unknown')})
  351.         assert len(values) == 1, values
  352.         assert values[0]['Name'] == None, values
  353.         assert values[0]['Access'] == None, values
  354.  
  355.         # Modify value by select conditions
  356.         values = self.tdb.Select(
  357.             tabname, None,
  358.             conditions={'Name': dbtables.ExactCond('Nifty.MP3')})
  359.         assert len(values) == 1, values
  360.         assert values[0]['Type'] == "MP3", values
  361.         assert values[0]['Access'] == "2", values
  362.  
  363.         # Make sure change applied only to select conditions
  364.         values = self.tdb.Select(
  365.             tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')})
  366.         assert len(values) == 1, values
  367.         assert values[0]['Type'] == "Word", values
  368.         assert values[0]['Access'] == "9", values
  369.  
  370.  
  371. def test_suite():
  372.     suite = unittest.TestSuite()
  373.     suite.addTest(unittest.makeSuite(TableDBTestCase))
  374.     return suite
  375.  
  376.  
  377. if __name__ == '__main__':
  378.     unittest.main(defaultTest='test_suite')
  379.